package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.PzdbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.PzdbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.PzdbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.PzdbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.PzdbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.PzdbgBsInfo;
import com.ruoyi.report_ljlm.mapper.PzdbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IPzdbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面平整度试验检测报告(连续式平整度仪)Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PzdbgBsInfoServiceImpl implements IPzdbgBsInfoService {

    private final PzdbgBsInfoMapper baseMapper;
    private final PzdbgBsDetailMapper detailMapper;

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)
     */
    @Override
    @FIllBasicInfo
    public PzdbgBsInfoVo queryById(Long excelId) {
        PzdbgBsInfoVo pzdbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<PzdbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<PzdbgBsDetail>().eq(PzdbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            pzdbgBsInfoVo.setDetailList(detailList);
        }
        return pzdbgBsInfoVo;
    }

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)列表
     */
    @Override
    public TableDataInfo<PzdbgBsInfoVo> queryPageList(PzdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PzdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<PzdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)列表
     */
    @Override
    public List<PzdbgBsInfoVo> queryList(PzdbgBsInfoBo bo) {
        LambdaQueryWrapper<PzdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PzdbgBsInfo> buildQueryWrapper(PzdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PzdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, PzdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, PzdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), PzdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), PzdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), PzdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), PzdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), PzdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), PzdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), PzdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), PzdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), PzdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), PzdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), PzdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), PzdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), PzdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getShejz() != null, PzdbgBsInfo::getShejz, bo.getShejz());
        lqw.eq(bo.getQvjs() != null, PzdbgBsInfo::getQvjs, bo.getQvjs());
        lqw.eq(bo.getHegl() != null, PzdbgBsInfo::getHegl, bo.getHegl());
        lqw.eq(bo.getPingjz() != null, PzdbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, PzdbgBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getBianyxs() != null, PzdbgBsInfo::getBianyxs, bo.getBianyxs());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), PzdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), PzdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), PzdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), PzdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), PzdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), PzdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), PzdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), PzdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), PzdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), PzdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路基路面平整度试验检测报告(连续式平整度仪)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(PzdbgBsInfoBo bo) {
        PzdbgBsInfo add = BeanUtil.toBean(bo, PzdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<PzdbgBsDetail> details = new ArrayList<>(21);
            for (int i = 1; i <= 21; i++) {
                details.add(new PzdbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路基路面平整度试验检测报告(连续式平整度仪)
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(PzdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        PzdbgBsInfo update = BeanUtil.toBean(bo, PzdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), PzdbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(PzdbgBsInfoBo bo) {
        if (!BigDecimalUtil.isAnyOneEmpty(bo.getShejz(), bo.getPingjz())) {
            if (BigDecimalUtil.isLessOrEqual(bo.getShejz(), bo.getPingjz())) {
                bo.setExcelResult(2);
                return;
            }
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PzdbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面平整度试验检测报告(连续式平整度仪)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PzdbgBsInfoVo jcjl(PzdbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标符合《公路沥青路面施工技术规范》 (JTG F40-2004)及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
